package vn.gq.udv.utils;


import vn.gq.udv.db.items.Category;
import vn.gq.udv.db.items.ChapterItem;
import vn.gq.udv.template.GQConst;
import vn.gq.udv.template.ReaderActivity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class Bookmark implements OnClickListener {
	
	public static final String PREF_CATEGORY_KEY;
	public static final String PREF_CHAPTER_KEY;
	public static final String PREF_NOTIFICATION_KEY;
	
	private SharePreferenceUtil pref;
	private TextView notifiTextView;
	private Context context;
	
	static {
		PREF_CATEGORY_KEY = "pref_category";
		PREF_CHAPTER_KEY = "pref_chapter";
		PREF_NOTIFICATION_KEY = "pref_notification";
	}
	
	public Bookmark(Context context) {
		this.context = context;
		pref = new SharePreferenceUtil(context);
	}
	
	public Bookmark(Context context, TextView tv) {
		this.context = context;
		pref = new SharePreferenceUtil(context);
		notifiTextView = tv;
		notifiTextView.setOnClickListener(this);
	}
	
	public void showNotificationOnStartUp() {
		showNotificationOnStartUp(false);
	}
	
	public void showNotificationOnStartUp(boolean showCatName) {
		if (getNotification()) {
			notifiTextView.setVisibility(View.VISIBLE);
			ChapterItem chapterItem = getChapterBookmark();
			if (chapterItem != null) {
				String notificationText = "Bạn đang đọc: ";
				if (showCatName) {
					Category cat = getCategoryBookmark();
					if (cat != null) {
						notificationText += cat.getCatName() + " > ";
					}
				}
				notificationText += chapterItem.getTitle();
				notifiTextView.setText(notificationText);
			} else {
				setNotification(false);
			}
		} else {
			notifiTextView.setVisibility(View.GONE);
		}
	}
	
	public void setCategoryBookmark(Category cat) {
		pref.putString(PREF_CATEGORY_KEY, cat.toJSONObject().toString());
	}
	
	public Category getCategoryBookmark() {
		Category cat = null;
		try {
			String json = pref.getString(PREF_CATEGORY_KEY, null);
			if (json != null) cat = new Category(json);
		} catch (Exception e) {
		}
		return cat;
	}
	
	public void setChapterBookmark(ChapterItem chapter) {
		pref.putString(PREF_CHAPTER_KEY, chapter.toJSonObject().toString());
		setNotification(true);
	}
	
	public void removeChapterBookmark() {
		pref.putString(PREF_CHAPTER_KEY, "");
		setNotification(false);
	}
	
	public ChapterItem getChapterBookmark() {
		ChapterItem chapter = null;
		try {
			chapter = new ChapterItem(pref.getString(PREF_CHAPTER_KEY, null));
		} catch (Exception e) {
		}
		return chapter;
	}
	
	public void setNotification(boolean value) {
		pref.putBoolean(PREF_NOTIFICATION_KEY, value);
	}
	
	public boolean getNotification() {
		return pref.getBoolean(PREF_NOTIFICATION_KEY, false);
	}
	
	@Override
	public void onClick(View v) {
		notifiTextView.setVisibility(View.GONE);
		setNotification(false);
		ChapterItem chapterItem = getChapterBookmark();
		Category cat = getCategoryBookmark();
		int catId = 0;
		if (GQConst.SHOW_CATEGORY) {
			if (cat != null)
				catId = cat.getCatId();
		}
		removeChapterBookmark();
		int scrollYPos = chapterItem.getScrollY();
		ReaderActivity.startReaderActivity(context, null, chapterItem == null ? 0 : chapterItem.getPosition(), catId, scrollYPos);
	}
}
